﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CustomException;
using Display.Properties;
using EnglishLibrary;
using DataTransfer;
using Global;

namespace Display
{
    public partial class VocabularyUC : UserControl
    {
        #region Properties
        //the datatranfer object
        VocabularyDT vocabularyDT = new VocabularyDT();
        //the list of vocabulary
        private List<Vocabulary> _vocabularyList = new List<Vocabulary>();
        //the current vocabulary
        private Vocabulary _crrVocabulary = new Vocabulary();
        public Vocabulary CrrVocabulary
        {
            get { return _crrVocabulary; }
            set { _crrVocabulary = value; }
        }
        public int Count
        {
            get
            {
                if (this._vocabularyList != null)
                    return this._vocabularyList.Count;
                return 0;
            }
        }
        #endregion

        #region Constructor and Load method
        public VocabularyUC()
        {
            InitializeComponent();
        }

        private void VocabularyUC_Load(object sender, EventArgs e)
        {
            //setup the delegate
            this.rateUC1.ChangeRate = new RateUC.SelectedIndexChanged(ChangeRate);
        }
        #endregion

        #region Functional methods
        public void Next()
        {
            if (_vocabularyList.Count <= 0)
                //throw new NoItemException(Resources.Exception_NoItem);
                return;

            //random the index of the vocabulary list
            var r = new Random();
            var index = r.Next(0, _vocabularyList.Count);

            //set the current vocabulary
            this.CrrVocabulary = _vocabularyList[index];

            //display the item
            this.lbWord.Text = this.CrrVocabulary.Word;
            this.lbPron.Text = this.CrrVocabulary.Pronunciation;
            this.rtbMeaning.Text = this.CrrVocabulary.Meaning;
            this.rtbExample.Text = this.CrrVocabulary.Example;

            //display this current rate
            this.rateUC1.SelectedIndex = Utility.RateIdToNumber(this.CrrVocabulary.RateID);
        }
        
        public void ChangeRate(string selectedValue)
        {
            if (this.CrrVocabulary.RateID != selectedValue)
            {
                this.CrrVocabulary.RateID = selectedValue;
                vocabularyDT.UpdateVocabulary(this.CrrVocabulary);
            }
        }

        public void LoadItems()
        {
            //get list of vocabularies from database
            if (Settings.IS_BEFORE_DATE_CHECKED && Settings.BEFORE_DATE != string.Empty)
                _vocabularyList = vocabularyDT.GetItemsBeforeDate(DateTime.Parse(Settings.BEFORE_DATE));
            else if (Settings.IS_AFTER_DATE_CHECKED && Settings.AFTER_DATE != string.Empty)
                _vocabularyList = vocabularyDT.GetItemsAfterDate(DateTime.Parse(Settings.AFTER_DATE));
            else if (Settings.CATEGORY_LIST_DISPLAY.Count > 0)
                _vocabularyList = vocabularyDT.GetItemsByCates(Settings.CATEGORY_LIST_DISPLAY.Keys.ToList());
            else
                _vocabularyList = vocabularyDT.GetListVocabulary(false);  //get all the items which are not deleted
        }
        public void DeleteCurrentItem()
        {
            if (_vocabularyList != null && _crrVocabulary != null && _vocabularyList.Contains(_crrVocabulary))
            {
                //delete the current item
                _vocabularyList.Remove(_crrVocabulary);
                //mark deleted in the database
                vocabularyDT.DeleteVocabulary(_crrVocabulary.ItemID);
            }
        }
        #endregion Functional methods

        #region Event handlers
        private void VocabularyUC_MouseEnter(object sender, EventArgs e)
        {
            ((TestReminder)this.Parent).MouseEnter();
        }

        private void VocabularyUC_MouseLeave(object sender, EventArgs e)
        {
            ((TestReminder)this.Parent).MouseLeave();
        }
        #endregion Event handlers
    }
}
